﻿using DeathByCaptcha;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using WatiN.Core;
using System.Net;
using System.IO;
using System.Collections.Specialized;
using System.Drawing.Imaging;
using TheArtOfDev.HtmlRenderer.WinForms;
using WatiN.Core.DialogHandlers;
using System.Drawing.Drawing2D;
using System.Text.RegularExpressions;
using VisaPoint.Controllers;
using VisaPoint.Entities;

namespace VisaPoint
{
    public partial class FrmVisapoint : System.Windows.Forms.Form, IVisapoint
    {
        VisapointClientController vispoinClient;
        Appointment appointment = new Appointment();
        public FrmVisapoint(Appointment appointment)
        {
            this.appointment = appointment;
            InitializeComponent();

            CheckForIllegalCrossThreadCalls = false;
        }

        Boolean IsStart = false;


        private void Form1_Load(object sender, EventArgs e)
        {
            //VisapointClientController.sendEmail("tuyennv.haui@gmail.com", "test", "test phat");
        }
  
        public void TimeTick(object sender, EventArgs e)
        {
            string time = (string)sender;
            lbTime.Text = time;
        }



        public void WriteLog(String message)
        {
            FileStream _stream;
            try
            {
                string pathFolder = Application.StartupPath + "\\Log\\";
                if (!Directory.Exists(pathFolder))
                {
                    Directory.CreateDirectory(pathFolder);
                }
                String Path = pathFolder + string.Format("{0:yyyy-MM-dd}", DateTime.Now) + ".txt";
                //String Path = HttpContext.Current.Server.MapPath("~/") + "200302.txt";
                if (!File.Exists(Path))
                    _stream = File.Create(Path);
                else
                    _stream = new FileStream(Path, FileMode.Append);

                StreamWriter sw = new StreamWriter(_stream);
                string content = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + ":" + message + Environment.NewLine;
                sw.WriteLine(content);
                rtbLog.AppendText(content);
                rtbLog.SelectionStart = rtbLog.Text.Length;
                rtbLog.ScrollToCaret();
                //sw.Flush();
                sw.Close();
                sw.Dispose();
                _stream.Close();
                _stream.Dispose();
            }
            catch (System.Exception ex)
            {
            }
        }

        //private void btnRun_Click(object sender, EventArgs e)
        //{
        //    if (!IsStart)
        //    {
        //        IsStart = true;
        //        appointment.Citizenship = "Thailand (ไทย)";
        //        appointment.Purpose = "Thị thực dài hạn theo mục đích đoàn tụ gia đình";
        //        appointment.FirstName = "Hieu";
        //        appointment.FamilyName = "Tran Manh";
        //        appointment.DateOfBirth = "29/01/1992";
        //        appointment.Email = "tuyennv.haui@gmail.com";
        //        appointment.PhoneNumber = "01689942158";
        //        appointment.PassportNumber = "D8913798";
        //        vispoinClient = new VisapointClientController(this, appointment, txtIP.Text);
        //        vispoinClient.AppointmentCurrent = appointment;
        //        vispoinClient.TimerHandler += new EventHandler(TimeTick);
        //        vispoinClient.Run();

        //        btnRun.Text = "Connected";
        //    }
        //    //else
        //    //{
        //    //    IsStart = false;

        //    //    vispoinClient.Stop();
        //    //    btnRun.Text = "Start";
        //    //    //if (browser != null)
        //    //    //{
        //    //    //    browser.Close();
        //    //    //    //browser.Dispose();
        //    //    //}
        //    //}
        //}


        public void Alert(string message)
        {
            MessageBox.Show(message);
        }

        public void CloseForm()
        {
            this.Close();
        }
        //private void button1_Click_1(object sender, EventArgs e)
        //{
        //    //appointment.Citizenship = "Thailand (ไทย)";
        //    //appointment.Purpose = "Thị thực dài hạn theo mục đích đoàn tụ gia đình";
        //    //appointment.FirstName = "Hieu";
        //    //appointment.FamilyName = "Tran Manh";
        //    //appointment.DateOfBirth = "29/01/1990";
        //    //appointment.Email = "tuyennv.haui@gmail.com";
        //    //appointment.PhoneNumber = "01689942158";
        //    //appointment.PassportNumber = "D8913798";
        //    //appointment.Gender = "Nam";
        //    vispoinClient = new VisapointClientController(this, appointment, "");
        //    vispoinClient.TimerHandler += new EventHandler(TimeTick);
        //    vispoinClient.AppointmentCurrent = appointment;
        //    vispoinClient.Status = VISAPOINT_STATUS.RUNNING;
        //    vispoinClient.Visapoint = this;
        //    vispoinClient.Run();
        //    //new Thread(new ThreadStart(test)).Start();
        //}

        private void FrmVisapoint_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (vispoinClient != null)
            {
                vispoinClient.Close();
            }
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            if (!IsStart)
            {
                IsStart = true;
                vispoinClient = new VisapointClientController(this, appointment, "",txtTimeSubmit);
                vispoinClient.txtTimeSubmit = txtTimeSubmit;
                vispoinClient.TimerHandler += new EventHandler(TimeTick);
                vispoinClient.AppointmentCurrent = appointment;
                vispoinClient.Status = VISAPOINT_STATUS.RUNNING;
                vispoinClient.Visapoint = this;
                vispoinClient.Run();
                btnRun.Text = "Stop";
                lbStatus.Text = "System is running........";
            }
            else
            {
                IsStart = false;
                vispoinClient.Stop();
                btnRun.Text = "Start";
                lbStatus.Text = "System is stoped........";
            }
        }
    }
}
